/*
* ============================================================================
* 功能描述 ：应用层配置
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/

#include "main.h"
#include <stdio.h>

#if 0
static void _temp_handle(signed short t, signed short h)
{
	unsigned char t10 = 0, t100 = 0, t1000 = 0;
	unsigned char h10 = 0, h100 = 0, h1000 = 0;
	long  j,j1,j2;

	t10 	= (t/10%10);		// 十位位
	t100 	= (t/100%10);		// 百位
	t1000 	= (t/1000);			// 千位
	DelayXms(2);
//	t_10 	= _temp_lcd(t10);
//	t_100 	= _temp_lcd(t100);
//	t_1000 	= _temp_lcd(t1000);

	j1  = SHORT_GET_HIGH_BYTE(h);
	j2  = SHORT_GET_LOW_BYTE(h);
	
	j  = j1*256+j2;

	h10		= (j/100%10); 		// bai位
	h100	= (j/1000%10);		// qian位	
	h1000	= (j/10000%10);		// wan位
	DelayXms(2);
//	h_10	= _temp_lcd(h10);
//	h_100	= _temp_lcd(h100);
//	h_1000	= _temp_lcd(h1000);

//	hp[0] = SHORT_GET_HIGH_BYTE(h_10);
//	hp[1] = SHORT_GET_LOW_BYTE(h_10);
//	hp[2] = SHORT_GET_HIGH_BYTE(h_100);
//	hp[3] = SHORT_GET_LOW_BYTE(h_100);
//	hp[4] = SHORT_GET_HIGH_BYTE(h_1000);
//	hp[5] = SHORT_GET_LOW_BYTE(h_1000);
	if(t1000 == 1)
	{
		if(t100 <= 8 )
		{
			if(h1000 == 4 || h1000 == 5)
			{
				Write_Lcd_Storage(face_state,1);
			}
			else if(h1000 == 6)
			{
				if(h100 < 5)
				{
					Write_Lcd_Storage(face_state,1);
				}
				else
					Write_Lcd_Storage(face_state,0);
			}
			else
				Write_Lcd_Storage(face_state,0);
		}
		else
			Write_Lcd_Storage(face_state,0);
	}
	else if(t1000 == 2)
	{
		if(t100 <= 8)
		{
			if(h1000 == 4 || h1000 == 5)
			{
				Write_Lcd_Storage(face_state,1);
			}
			else if(h1000 == 6)
			{
				if(h100 < 5)
				{
					Write_Lcd_Storage(face_state,1);
				}
				else
					Write_Lcd_Storage(face_state,0);
			}
			else
				Write_Lcd_Storage(face_state,0);
		}
		else
			Write_Lcd_Storage(face_state,0);
	}
	else
		Write_Lcd_Storage(face_state,0);
	
#if 0
	char a1[2],a2[2],a3[2];
	a1[0] = h10;
	a2[0] = h100;
	a3[0] = h1000;
	DelayXms(200);
	Uart_Tx_buf(a1, 1);		// 打印每一位值
	DelayXms(200);
	Uart_Tx_buf(a2, 1);
	DelayXms(200);
	Uart_Tx_buf(a3, 1);
#endif
	//Lcd_Set_temp(t_1000, t_100, t_10);
	Write_Lcd_Storage(temp_1,t10);
	DelayXms(2);
	Write_Lcd_Storage(temp_10,t100);
	DelayXms(2);
	Write_Lcd_Storage(temp_100,t1000);
	DelayXms(2);
	Write_Lcd_Storage(hum_10,h100);
	DelayXms(2);
	Write_Lcd_Storage(hum_100,h1000);
	Seg_Set_Data();
}
#endif
static void _temp_handle(signed short t, signed short h)
{
    // 提取温度和湿度的位值
    unsigned char t10 = t / 10 % 10, t100 = t / 100 % 10, t1000 = t / 1000;
	long j = (SHORT_GET_HIGH_BYTE(h) * 256 + SHORT_GET_LOW_BYTE(h)) ;
    unsigned char h10 = h / 100 % 10,
                 h100 = h / 1000 % 10,
                 h1000 = h / 10000 % 10;

   

    // 设置面部状态
    if (t1000 >= 1 && t1000 <= 2 && t100 <= 8) {
        if ((h100 == 4 || h100 == 5) || (h100 == 6 && h10 < 5))
            Write_Lcd_Storage(face_state, 1);
        else
            Write_Lcd_Storage(face_state, 0);
    } else {
        Write_Lcd_Storage(face_state, 0);
    }
	 // 显示温度和湿度
    Write_Lcd_Storage(temp_1, t10);
    Write_Lcd_Storage(temp_10, t100);
    Write_Lcd_Storage(temp_100, t1000);		
	
	Write_Lcd_Storage(hum_10, h10);
   		Write_Lcd_Storage(hum_100, h100);
	
    // 旧代码注释已移除，如果需要调试或输出，可以重新启用
#if 0
if(h1000 > 0)
	{ 
		Write_Lcd_Storage(hum_10, h100);
   		Write_Lcd_Storage(hum_100, h1000);

	}
	if(h1000 == 0)
	{
		Write_Lcd_Storage(hum_10, h10);
	   	Write_Lcd_Storage(hum_100, h100);
	}
    // Debugging or output code
    char a1[2], a2[2], a3[2];
    a1[0] = h10;
    a2[0] = h100;
    a3[0] = h1000;
    DelayXms(200);
    Uart_Tx_buf(a1, 1);
    DelayXms(200);
    Uart_Tx_buf(a2, 1);
    DelayXms(200);
    Uart_Tx_buf(a3, 1);
#endif

    Seg_Set_Data();
}
void Hly_Temp_Hum_Obtain(void)
{
    signed short t, h;
	char i;
	signed short ak_47;
    unsigned char j[3];
    unsigned char Data_f;

    Data_f = SHT4XReadTemperatureAndHumidity(&t, &h); // 获取温湿度

    if (Data_f == 1) {
	j[0] = h >> 8;//湿度高8位
	j[1] = (unsigned char)h;//湿度低8位
	ak_47 = j[0]*256 + j[1];

	
    // 发送温湿度数据
    Hly_Packet_device_manage(Report_specified_type, 6, t, 0);
    DelayXms(2);
    Hly_Packet_device_manage(Report_specified_type, 6, h, 1);


    // 处理温湿度的每一位值
    _temp_handle(t, h);

    // 注释掉未使用的变量和发送代码，可以考虑移除它们
    // temp[0]  =  SHORT_GET_HIGH_BYTE(t);
    // temp[1]  =  SHORT_GET_LOW_BYTE(t);
    // hum[0]	 =  SHORT_GET_HIGH_BYTE(h);
    // hum[1]	 =  SHORT_GET_LOW_BYTE(h);
    
    // DelayXms(200);
    // Uart_Tx_buf(temp, 2);
    // Uart_Tx_buf(hum, 2);
    }
}


void App_Run(void)
{
	hly_time_start();
	Zigbee_Cmd_handle();
	Zigbee_Config_Set();

}

void app_init(void)
{
	Hly_Packet_Network_msg(query_current_network_status, 0, 0);
	DelayXms(10);
	Hly_Temp_Hum_Obtain();
}

